from PyQt5.QtGui import QPalette, QColor
from PyQt5.QtWidgets import QApplication
from .platform_detect import LinuxEnv

def apply_theme(app):
    """跨平台主题适配"""
    de = LinuxEnv.get_desktop_env()
    
    # 基础样式
    app.setStyle("Fusion")
    palette = app.palette()
    
    # 根据环境调整
    if de == "deepin":
        # Deepin 深色模式适配
        palette.setColor(QPalette.Window, QColor(45, 45, 45))
        palette.setColor(QPalette.WindowText, Qt.white)
        palette.setColor(QPalette.Base, QColor(35, 35, 35))
    elif de == "kde":
        # KDE 默认调色板
        palette.setColor(QPalette.Window, QColor(239, 240, 241))
        palette.setColor(QPalette.WindowText, Qt.black)
    else:
        # GNOME及其他默认
        palette.setColor(QPalette.Window, QColor(255, 255, 255))
    
    app.setPalette(palette)
    
    # 动态样式表
    app.setStyleSheet("""
    QLabel#titleBar {
        background: qlineargradient(x1:0, y1:0, x2:0, y2:1, 
            stop:0 #3D3D3D, stop:1 #2D2D2D);
        color: white;
        border-radius: 4px;
    }
    """)